//
//  FourthViewController.m
//  UIhomework_14
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "FourthViewController.h"
#import "FourthViewController2.h"
#import "ActiviityViewController.h"
#import "MovieViewController.h"
#import "DataBaseHandle.h"
#import "TitleModel.h"
@interface FourthViewController ()<UITableViewDelegate, UITableViewDataSource, FourDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UIBarButtonItem *item;
@end

@implementation FourthViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

-(void)dealloc{
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.tintColor = [UIColor cyanColor];
    self.navigationItem.title = @"我的";
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [_tableView release];
    
  self.item = [[UIBarButtonItem alloc] initWithTitle:@"登陆" style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];
    self.navigationItem.rightBarButtonItem = _item;
   
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//协议部分
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    if (indexPath.row == 0) {
        cell.textLabel.text = @"我的活动";
        return cell;
    }
    if (indexPath.row == 1) {
        cell.textLabel.text = @"我的电影";
        return cell;
    }
    if (indexPath.row == 2) {
        cell.textLabel.text = @"清除缓存";
        return cell;
    }
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 2) {
        //删除活动数据
        TitleModel *model = [TitleModel titleModel];
        [[DataBaseHandle shareDataBaseHandle] deleteModel:model];
        
        //删除电影数据
        MovieModel *movieModel = [MovieModel movieModel];
        [[DataBaseHandle shareDataBaseHandle] deleteMovieModel:movieModel];
        
        
        
        
         UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"清楚缓存成功" preferredStyle:UIAlertControllerStyleAlert];
         UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:nil];
    }
    if (indexPath.row == 0) {
        ActiviityViewController *act = [[ActiviityViewController alloc] init];
        [self.navigationController pushViewController:act animated:YES];
        [act release];
    }
    if (indexPath.row == 1) {
        MovieViewController *movie = [[MovieViewController alloc] init];
        [self.navigationController pushViewController:movie animated:YES];
        [movie release];
    }
    
    }
- (void)itemAction:(UIBarButtonItem *)itemAction{
    if (![_item.title  isEqual: @"注销"]) {
        FourthViewController2 *controller = [[FourthViewController2 alloc] init];
        controller.delegate = self;
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:controller];
        [self presentViewController:nav animated:YES completion:^{
            
        }];
    }else{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"注销成功" preferredStyle:UIAlertControllerStyleAlert];
//        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            _item.title = @"注册";
        }];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:action];
        [alert addAction:action1];
        [self presentViewController:alert animated:YES completion:nil];
    }
   
}
-(void)chuanzhi{
//    _item.title = @"注销";
//    self.navigationController.navigationItem.rightBarButtonItem.title = @"注销";
    
    _item.title = @"注销";
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
